<?php

namespace App\Console\Commands;

use App\Models\Banner;
use App\Models\Category;
use App\Models\Config;
use App\Models\Cover;
use App\Models\Index;
use App\Models\Tag;
use Illuminate\Console\Command;

class FillData extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'fill:data';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '填充数据';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        Config::query()->truncate();
        Config::query()->create([
            'key'  => 'signIntegral',
            'name' => '签到积分',
            'val'  => '6'
        ]);

        Config::query()->create([
            'key'  => 'inviteIntegral',
            'name' => '邀请好友积分',
            'val'  => '6'
        ]);

        Config::query()->create([
            'key'  => 'adIntegral',
            'name' => '广告积分',
            'val'  => '6'
        ]);

        Config::query()->create([
            'key'  => 'downIntegral',
            'name' => '下载扣除积分',
            'val'  => '0'
        ]);

        Config::query()->create([
            'key'  => 'writeShow',
            'name' => '开启投稿',
            'val'  => 'true'
        ]);

        Config::query()->create([
            'key'  => 'writeExam',
            'name' => '投稿需审核',
            'val'  => 'false'
        ]);

        Banner::query()->truncate();
        $json = json_decode(file_get_contents(public_path('db_init.json')), true);
        $wx_banner = $json['wx_banner']['data'];
        $banner = [];
        foreach ($wx_banner as $item) {
            $banner[] = [
                'image' => $item['image'],
                'sort'  => $item['sort'],
                'type'  => 0,
                'url'   => $item['url'],
                'appid' => '',
                'path' => '',
                'state' => 1,
                'created_at' => now(),
                'updated_at' => now(),
            ];
        }
        Banner::query()->insert($banner);


        $wx_index = $json['wx_index']['data'];
        $index = [];
        foreach ($wx_index as $item) {
            $index[] = [
                'image' => $item['image'],
                'name'  => $item['name'],
                'sort'  => $item['sort'],
                'type'  => 0,
                'url'   => $item['url'],
                'state' => 1,
                'created_at' => now(),
                'updated_at' => now(),
            ];
        }
        Index::query()->insert($index);

        $wx_index1 = $json['wx_index1']['data'];
        $index1 = [];
        foreach ($wx_index1 as $item) {
            $index1[] = [
                'image' => $item['image'],
                'name'  => $item['name'],
                'sort'  => $item['sort'],
                'type'  => 1,
                'url'   => $item['url'],
                'state' => 1,
                'created_at' => now(),
                'updated_at' => now(),
            ];
        }
        Index::query()->insert($index1);

        Cover::query()->truncate();
        $wx_cover = $json['wx_cover']['data'][0];
        $cover = [
            'name' => $wx_cover['name'],
            'user_id' => 1,
            'category_id' => 5,
            'category_name' => $wx_cover['category_name'],
            'image' => $wx_cover['image'],
            'images' => implode(',',$wx_cover['images']),
            'mode' => $wx_cover['mode'],
            'status' => $wx_cover['status'],
            'tags' => $wx_cover['tags'],
            'view' => $wx_cover['view'],
            'zan' => '',
        ];

        Cover::query()->create($cover);

        Category::query()->truncate();
        $wx_category = $json['wx_category']['data'];
        $category = [];
        foreach ($wx_category as $item) {
            $category[] = [
                'image' => $item['image'],
                'name'  => $item['name'],
                'sort'  => $item['sort'],
                'created_at' => now(),
                'updated_at' => now(),
            ];
        }
        Category::query()->insert($category);



        Tag::query()->truncate();
        $wx_tags = $json['wx_tags']['data'];
        $tags = [];
        foreach ($wx_tags as $item) {
            $tags[] = [
                'name'  => $item['name'],
                'created_at' => now(),
                'updated_at' => now(),
            ];
        }
        Tag::query()->insert($tags);

        return 0;
    }
}
